package net.jsoft.platform.monitor.util;

import java.io.InputStreamReader;
import java.io.LineNumberReader;

public class WindowsUtils
{
    //可以设置长些，防止读到运行此次系统检查时的cpu占用率，就不准了   
    private static final int CPUTIME = 5000;

    private static final int PERCENT = 100;

    private static final int FAULTLENGTH = 10;

    /** */
    /**  
    * 获得CPU使用率.  
    * @return 返回cpu使用率  
    * @author amg     * Creation date: 2008-4-25 - 下午06:05:11  
    */
    public static double getCpuRatioForWindows()
    {
        try
        {
            String procCmd = System.getenv("windir") + "//system32//wbem//wmic.exe process get Caption,CommandLine," + "KernelModeTime,ReadOperationCount,ThreadCount,UserModeTime,WriteOperationCount";
            // 取进程信息   
            long[] c0 = readCpu(Runtime.getRuntime().exec(procCmd));
            Thread.sleep(CPUTIME);
            long[] c1 = readCpu(Runtime.getRuntime().exec(procCmd));
            if (c0 != null && c1 != null)
            {
                long idletime = c1[0] - c0[0];
                long busytime = c1[1] - c0[1];
                return Double.valueOf(PERCENT * (busytime) / (busytime + idletime)).doubleValue();
            }
            else
            {
                return 0.0;
            }
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
            return 0.0;
        }
    }

    /** */
    /**  
     * 获得CPU使用率.  
     * @return 返回cpu使用率  
     * @author amg     * Creation date: 2008-4-25 - 下午06:05:11  
     */
    public static long getCpuUsedForWindows()
    {
        long[] c0 = null;
        try
        {
            String procCmd = System.getenv("windir") + "//system32//wbem//wmic.exe process get Caption,CommandLine," + "KernelModeTime,ReadOperationCount,ThreadCount,UserModeTime,WriteOperationCount";
            // 取进程信息   
            c0 = readCpu(Runtime.getRuntime().exec(procCmd));
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
        if (c0 != null && c0.length > 2)
        {
            return c0[1];
        }
        else
        {
            return 0;
        }

    }

    /** */
    /** 
    * 读取CPU信息. 
    * @param proc 
    * @return 
    * @author amg     * Creation date: 2008-4-25 - 下午06:10:14 
    */
    private static long[] readCpu(final Process proc)
    {
        long[] retn = new long[2];
        try
        {
            proc.getOutputStream().close();
            InputStreamReader ir = new InputStreamReader(proc.getInputStream());
            LineNumberReader input = new LineNumberReader(ir);
            String line = input.readLine();
            if (line == null || line.length() < FAULTLENGTH)
            {
                return null;
            }
            int capidx = line.indexOf("Caption");
            int cmdidx = line.indexOf("CommandLine");
            int rocidx = line.indexOf("ReadOperationCount");
            int umtidx = line.indexOf("UserModeTime");
            int kmtidx = line.indexOf("KernelModeTime");
            int wocidx = line.indexOf("WriteOperationCount");
            long idletime = 0;
            long kneltime = 0;
            long usertime = 0;
            while ((line = input.readLine()) != null)
            {
                if (line.length() < wocidx)
                {
                    continue;
                }
                // 字段出现顺序：Caption,CommandLine,KernelModeTime,ReadOperationCount,  
                // ThreadCount,UserModeTime,WriteOperation  
                String caption = substring(line, capidx, cmdidx - 1).trim();
                String cmd = substring(line, cmdidx, kmtidx - 1).trim();
                if (cmd.indexOf("wmic.exe") >= 0)
                {
                    continue;
                }
                // log.info("line="+line);  
                if (caption.equals("System Idle Process") || caption.equals("System"))
                {
                    idletime += Long.valueOf(substring(line, kmtidx, rocidx - 1).trim()).longValue();
                    idletime += Long.valueOf(substring(line, umtidx, wocidx - 1).trim()).longValue();
                    continue;
                }

                kneltime += Long.valueOf(substring(line, kmtidx, rocidx - 1).trim()).longValue();
                usertime += Long.valueOf(substring(line, umtidx, wocidx - 1).trim()).longValue();
            }
            retn[0] = idletime;
            retn[1] = kneltime + usertime;
            return retn;
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
        finally
        {
            try
            {
                proc.getInputStream().close();
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
        return null;
    }

    private static String substring(String src, int start_idx, int end_idx)
    {
        byte[] b = src.getBytes();
        String tgt = "";
        for (int i = start_idx; i <= end_idx; i++)
        {
            tgt += (char) b[i];
        }
        return tgt;
    }

}
